package xq.gwt.mvc.model;

public abstract class PersistableEntityModel extends AbstractEntityModel {
	
	private static final long serialVersionUID = -8609306484477758113L;
	
	public abstract PropertyModel[] getKeyProperties();
	
	public abstract PersistableEntityModel newInstance();
	
	@Override
	public PersistableEntityModel copy()throws Exception{
		PersistableEntityModel newEntity = newInstance();
		copyPropertyValues(this, newEntity);		
		return newEntity;
	}
			
	@Override
	public String getIdentifier(){
		String identifier = null;
		PropertyModel[] keys = getKeyProperties(); 
		if (keys != null){
			for (int i = 0; i < keys.length; i++) {
				if(i == 0){
					identifier = keys[i].getText();
				}else
					identifier += keys[i].getText();
			}
		}
			
		return identifier;
	}
	
	public boolean isNew(){
		PropertyModel[] keys = getKeyProperties();
		if(keys == null)
			return true;
		for (int i = 0; i < keys.length; i++) {
			if(keys[i].getObjectValue() != null)
				return false;
		}
		
		
		return true;
	}
}
